/* 
  Make sure to add your name if you work on this page
  
  Copyright 2008 David Matthew Burke
  
    This file is part of Volunteer Tracker.

    Volunteer Tracker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Volunteer Tracker is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Volunteer Tracker.  If not, see <http://www.gnu.org/licenses/>.

 
 Important, when running on Mono make sure to allow the connection to google.
 Easy way:  sudo mozroots --import --machine 
 Better way:  read http://www.mono-project.com/FAQ:_Security 
*/


using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using Google.GData.Calendar;
using MySql.Data.MySqlClient;
using Google.GData.Extensions;
using Google.GData.Client;

namespace VolunteerTracker
{
	
	
	public partial class @default : System.Web.UI.Page
	{
		ArrayList entryList;
		DataTable events;
		int i=0;
		
		void Page_Load()
		{
			
			// Enter connection strings here
			string calendarURI = "http://www.google.com/calendar/feeds/9o5ku84joov3pdkv5fiujkaqh0@group.calendar.google.com/public/full";
         string userName = "";
			string passWord = "";   
			
			//Make DataTable to hold all the data we get
         entryList = new ArrayList(50);
         ArrayList dates = new ArrayList(50);
         ArrayList names = new ArrayList(50);
			
			events = new DataTable();
			DataColumn myDataColumn;
			
			myDataColumn = new DataColumn();
			myDataColumn.DataType = Type.GetType("System.String");
			myDataColumn.ColumnName = "name";
			events.Columns.Add(myDataColumn);
			
			myDataColumn = new DataColumn();
			myDataColumn.DataType = Type.GetType("System.String");
			myDataColumn.ColumnName = "date";
			events.Columns.Add(myDataColumn);
			
			EventQuery query = new EventQuery();
         
			CalendarService service = new CalendarService("CalendarSampleApp");
			
			//Do not set if no password is used (meaning it's public)
			if(userName != null && userName.Length > 0)
			{
				service.setUserCredentials(userName, passWord);
			}
			
			query.Uri = new Uri(calendarURI);
			query.StartTime = DateTime.Now.AddDays(-28); 
         query.EndTime = DateTime.Now.AddMonths(6);
			
			Google.GData.Calendar.EventFeed calFeed = service.Query(query) as EventFeed;
			
			// now populate the calendar
			while (calFeed != null && calFeed.Entries.Count > 0)
			{
				foreach (Google.GData.Calendar.EventEntry entry in calFeed.Entries)
				{
					entryList.Add(entry);
					
					DataRow row = events.NewRow();
					row["name"] = entry.Title.Text;
					row["date"] = entry.Times[0].StartTime.ToString();
					events.Rows.Add(row);	
				}
				// just query the same query again.
				if (calFeed.NextChunk != null)
				{
				  query.Uri = new Uri(calFeed.NextChunk); 
				  calFeed = service.Query(query) as EventFeed;
				}
				else
					calFeed = null;
			}
			
			if(!Page.IsPostBack)
			{
				rptEvents.DataSource = events;
				rptEvents.DataBind();
			}
		}
			
		protected void rptEvents_ItemCommand(object source, EventArgs e)
		{
			Button a = (Button)source;
			int number = Convert.ToInt32(a.Attributes["number"]);
			
			string eventId = dbConnect.LookUpEvent(events.Rows[number]["name"].ToString(), events.Rows[number]["date"].ToString());
			string userId = dbConnect.LookUserId(Request.ServerVariables["LOGON_USER"]);
			
			if(userId == "")
			{
				lblError.Text="You must log in to attend an event";
			}
			else if(eventId == "")
			{
				lblError.Text="Error, could not find event";
			}
			else
			{
				lblError.Text="Added event";
				//dbConnect.addEventToUser(eventId, userId);
			}
		}
		
		protected void onDataBound(object Sender, RepeaterItemEventArgs e)
		{
		   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
		   {
				Button button = e.Item.FindControl("btnAttend") as Button;
				if(button != null)
				{
					button.Attributes.Add("number", i.ToString());
					i++;
				}
			}
		}
	}
}
